package com.descartes.mymedia.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.descartes.mymedia.model.Profil;
import com.descartes.mymedia.service.ProfilService;
import com.descartes.mymedia.service.ProfilServiceImpl;

@Controller
public class Connexion {

	@RequestMapping(value="/connexion", method=RequestMethod.GET)
	public ModelAndView get(){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("profil", new Profil());
		modelAndView.setViewName("connexion");
		return modelAndView;
	}

	@RequestMapping(value="/connexion", method=RequestMethod.POST)
	public ModelAndView post(@RequestParam String pseudo, @RequestParam String mdp, HttpServletRequest request) throws Exception{
		ProfilService profilService = new ProfilServiceImpl();
		Profil profil = profilService.getProfil(pseudo, mdp);
		if(null != profil || !"".equals(profil)) {
			ModelAndView modelAndView = new ModelAndView("viewProfil");
			modelAndView.addObject("profil", profil);
			HttpSession session = request.getSession();
			session.setAttribute("profil", profil);
			return modelAndView;
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("connexion");
		return modelAndView;
	}
}
